// src/store/user.ts
import { defineStore } from 'pinia'
import type { UserState } from '@/types/user'

const initState = (): UserState => ({
  token: '',
  username: '',
  isLoggedIn: false,
})

export const useUserStore = defineStore<'user', UserState, {}, {
  setUser(token: string, username: string): void
  logout(): void
}>('user', {
  state: initState,
  actions: {
    setUser(token, username) {
      this.token = token
      this.username = username
      this.isLoggedIn = true
    },
    logout() {
      this.$patch(initState())   // 一键重置
    },
  },
})
